package com.cd2cd.code_template.service.impl;

import com.cd2cd.code_template.code_template.domain.SysAuth;
import com.cd2cd.code_template.code_template.domain.gen.SysAuthCriteria;
import com.cd2cd.code_template.code_template.mapper.SysAuthMapper;
import com.cd2cd.code_template.configuration.constants.ServiceCode;
import com.cd2cd.code_template.service.AuthConfigurationService;
import com.cd2cd.code_template.util.BeanUtil;
import com.cd2cd.code_template.vo.BaseRes;
import com.cd2cd.code_template.vo.SysAuthVo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.sql.Array;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Component
public class AuthConfigurationServiceImpl implements AuthConfigurationService {

    @Resource
    private SysAuthMapper sysAuthMapper;


    @Override
    public BaseRes<List<SysAuthVo>> getAuthList() {

        BaseRes<List<SysAuthVo>> res = new BaseRes<>(ServiceCode.SUCCESS);

        long count = sysAuthMapper.countByExample(null);

        if (count > 0) {
            List<SysAuth> sysAuths = sysAuthMapper.selectByExample(null);
            List<SysAuthVo> sysAuthVos = BeanUtil.voConvertList(sysAuths, SysAuthVo.class);
            Collections.sort(sysAuthVos);
            List<SysAuthVo> sysAuthNode = getSysAuthNode(sysAuthVos, 0);

            res.setData(sysAuthNode);
        }

        return res;
    }

    private List<SysAuthVo> getSysAuthNode(List<SysAuthVo> authList, Integer pid) {
        List<SysAuthVo> list = new ArrayList<>();

        for (SysAuthVo sysAuthVo : authList) {
            if (sysAuthVo.getPid().equals(pid)) {
                Integer id = sysAuthVo.getId();
                List<SysAuthVo> sysAuthNode = getSysAuthNode(authList, id);
                if (sysAuthNode.size() > 0) {
                    sysAuthVo.setChildren(sysAuthNode);
                }
                list.add(sysAuthVo);
            }
        }

        return list;
    }


    @Override
    public BaseRes<String> addAuth(SysAuthVo sysAuthVo) {

        sysAuthMapper.insertSelective(sysAuthVo);

        return new BaseRes<>(ServiceCode.SUCCESS);
    }

    @Override
    public BaseRes<String> updateAuth(SysAuthVo sysAuthVo) {

        BaseRes<String> res = new BaseRes<>(ServiceCode.SUCCESS);

        Integer pid = sysAuthVo.getPid();
        SysAuth sysAuth = sysAuthMapper.selectByPrimaryKey(sysAuthVo.getId());

        Integer oldSort = sysAuth.getSort();
        Integer newSort = sysAuthVo.getSort();


        if (newSort.equals(oldSort)) {
            sysAuthMapper.updateByPrimaryKey(sysAuthVo);
            return res;
        }

        // 排序顺序 后移 前移
        SysAuthCriteria criteria = new SysAuthCriteria();
        criteria.createCriteria().andPidEqualTo(pid).andIdNotEqualTo(sysAuth.getId());

        List<SysAuth> sysAuths = sysAuthMapper.selectByExample(criteria);

        /**
         *  0 1 2 3 4 5 6 7 8
         *  3 -> 6
         *  需要区间 (3,6}  -1
         *  3 -> 0
         *  需要区间 {0,3)  +1
         */

        for (SysAuth auth : sysAuths) {
            Integer itemSort = auth.getSort();

            if (oldSort < newSort) {
                // 区间 -1
                if (oldSort < itemSort && itemSort <= newSort) {
                    auth.setSort(itemSort -1);
                    sysAuthMapper.updateByPrimaryKeySelective(auth);
                }
            }else {
                // 区间 +1
                if (newSort <= itemSort && itemSort < oldSort) {
                    auth.setSort(itemSort + 1);
                    sysAuthMapper.updateByPrimaryKeySelective(auth);
                }
            }
        }

        // 判断排序号是否超出范围
        if (newSort >= sysAuths.size()) {
            sysAuthVo.setSort(sysAuths.size() -1);
        }

        sysAuthMapper.updateByPrimaryKeySelective(sysAuthVo);

        return res;
    }


    @Override
    public BaseRes<String> delAuth(SysAuthVo sysAuthVo) {
        Integer id = sysAuthVo.getId();
        delRelationAuth(id);
        sysAuthMapper.deleteByPrimaryKey(id);
        return new BaseRes<>(ServiceCode.SUCCESS);
    }

    // 递归删除权限
    private void delRelationAuth(Integer pid) {
        SysAuthCriteria criteria = new SysAuthCriteria();
        criteria.createCriteria().andPidEqualTo(pid);

        List<SysAuth> sysAuths = sysAuthMapper.selectByExample(criteria);

        for (SysAuth sysAuth : sysAuths) {
            Integer id = sysAuth.getId();
            delRelationAuth(id);
        }

        if (sysAuths.size() > 0) {
            sysAuthMapper.deleteByExample(criteria);
        }
    }


    @Override
    public BaseRes<SysAuthVo> authInfo(SysAuthVo sysAuthVo) {

        BaseRes<SysAuthVo> res = new BaseRes<>(ServiceCode.SUCCESS);

        SysAuth sysAuth = sysAuthMapper.selectByPrimaryKey(sysAuthVo.getId());

        if (null != sysAuth) {
            SysAuthVo authVo = BeanUtil.voConvert(sysAuth, SysAuthVo.class);
            res.setData(authVo);
        }

        return res;
    }
}
